<template>
    <div class="card-box">
        <div class="card-title">
            <span>政府投资项目</span>
            <el-col :span="2" v-show='showButton'>
                <el-button type="text" @click="addRow">新增</el-button>  |
                <el-button  type="text" @click="delRow">删除</el-button>
            </el-col>

        </div>
        <div class="margin-box">
            <!--<el-table :data="commitSelectRow" align="center"  @selection-change="changeSelect">
                <el-table-column type="selection" width="80"></el-table-column>
                <el-table-column label="序号" width="100" type="index"></el-table-column>
                <el-table-column label="建设单位" prop="constructionUnit"></el-table-column>
                <el-table-column label="建设开始日期" prop="constructionStartDate"></el-table-column>
                <el-table-column label="竣工日期" prop="completionDate"></el-table-column>
                <el-table-column label="资产占用单位" prop="assetOccupationUnit"></el-table-column>
                <el-table-column label="上级主管单位" prop="supervisor"></el-table-column>
                <el-table-column label="资产使用单位" prop="assetUseUnit"></el-table-column>
            </el-table>-->
            <el-table :data="commitSelectRow" align="center" >
                <el-table-column label="建设单位" prop="constructionUnit"></el-table-column>
                <el-table-column label="建设开始日期" prop="constructionStartTime" :formatter="dataFormat"></el-table-column>
                <el-table-column label="竣工日期" prop="completedTime" :formatter="dataFormat"></el-table-column>
                <el-table-column label="资产占用单位" prop="assetOccupyingUnit"></el-table-column>
                <el-table-column label="上级主管单位" prop="superiorCompetentUnit"></el-table-column>
                <el-table-column label="资产使用单位" prop="assetUseUnit"></el-table-column>
            </el-table>
        </div>

        <!--<v-dialog
            title="选择政府投资项目"
            :show.sync="dialogVisible"
            :closeOnClickModal="false"
            @beforeClose="beforeClose"
            width="800px">
            <div style="padding: 5px">
                <list-page
                    class="box-shadow"
                    :pager="pager"
                    :showSelectSearch="false"
                    placeholder="请输入政府投资项目"
                    @searchByKey="searchByKey"
                    @selectionChange="selectionChange"
                    @changePageSize="changePageSize"
                    @changeCurrentPage="changeCurrentPage">
                    <template slot="table">
                        <el-table
                            :data="roomData"
                            align="center"
                            @selection-change="selectionChange">
                            <el-table-column type="selection" width="55"></el-table-column>
                            <el-table-column type="index" label="序号" width="55"></el-table-column>
                            <el-table-column label="建设单位" prop="constructionUnit"></el-table-column>
                            <el-table-column label="建设开始日期" prop="constructionStartDate"></el-table-column>
                            <el-table-column label="竣工日期" prop="completionDate"></el-table-column>
                            <el-table-column label="资产占用单位" prop="assetOccupationUnit"></el-table-column>
                            <el-table-column label="上级主管单位" prop="supervisor"></el-table-column>
                            <el-table-column label="资产使用单位" prop="assetUseUnit"></el-table-column>
                        </el-table>
                    </template>
                </list-page>
            </div>
            <span slot="foot">
                <el-button @click="cancel()">取 消</el-button>
                <el-button @click="commit">保 存</el-button>
            </span>
        </v-dialog>-->

    </div>
</template>

<script>
import {getAssetRegistration} from '@/views/property-info-sys/proprietary-information/api/propertietar'
import pager from "@/assets/js/page-mixin";
import {queryPage} from '@/views/property-info-sys/property-manage/api/room.js'
import BaseLoad from '@/assets/vue-mixin/BaseLoad'
let moment = require('moment')

export default {
    name: "government-investment-projects",
    mixins: [pager, BaseLoad],
    props: {
        pid: {
            type: String
        },
        showButton: {
            type: Boolean,
            default: true
        },
        projectId:{
            type: String,
            default: ''
        },
        assetRegistrationId: {
            type: String,
            default: ''
        },
        governmentInvestmentId: {
            type: String
        }
    },
    mounted() {

    },
    data() {
        return {
            commitSelectRow: [],
            tempSelectRow: [],
            deleteRow: [],
            propertyName: '',
            pager: pager,
            roomData: [],
            dialogVisible: false
        }
    },
    methods: {
        // hxf-0923-政府联动出来
        handleChangeAssetRegistration(val) {
            getAssetRegistration(val).then(res => {
                if (res.data.code === 0 && res.data.data.assetRegistrationId !== null) {
                    this.commitSelectRow = [res.data.data]
                }
            })
        },
        // hxf-0923-格式化
        dataFormat:function(row,column,cellValue) {
            if (!cellValue) {
                return ''
            }
            return moment(cellValue).format('YYYY-MM-DD')
        },
        addRow(){
            if(this.governmentInvestmentId === ''){
                this.$alert('暂无政府投资项目', '提示', {
                    confirmButtonText: '确定',
                })
            } else {
                this.handleChangeAssetRegistration(this.governmentInvestmentId)
            }
        },
        delRow(){
            this.$confirm('是否删除政府投资项目?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'warning'
            }).then(() => {
                this.commitSelectRow = []
                this.$message.success('删除成功！')
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        /* beforeClose(done) {
            this.cancel()
            done()
        },
         selectionChange(row){
            //追加
            this.tempSelectRow = row
        },
        changeSelect(row){
            this.deleteRow = row
        },
        cancel() {
            this.tempSelectRow = []
            this.dialogVisible = false
        },
        commit(){
            this.commitSelectRow = this.commitSelectRow.concat(this.tempSelectRow)
            this.cancel()
        },
        searchByKey(val){
             console.log(val)
             this.propertyName = val
             this.roomQuery();
         },
         roomQuery(){
             let queryDont = this.commitSelectRow.map(ro => {
                 return ro.roomId
             })
             this.loadAction('数据加载中')
             queryPage(Object.assign({
                 current: pager.currentPage,
                 size: pager.pageSize,
                 dontUse: 'true',
                 allQuery: 'true',
                 dontQueryId: queryDont,
                 projectId: this.projectId
             }, {propertyName: this.propertyName})).then(rep => {
                 if(rep.data.code === 0){
                     let repD = rep.data.data
                     this.roomData = repD.records
                     this.pager.total = repD.total
                 }
                 this.loadClose()
             }).catch(e => {
                 this.loadClose()
             })
         },
         changePageSize(size){
             this.pager.pageSize = size
             this.roomQuery();
         },
         changeCurrentPage(page){
             this.pager.currentPage = page
             this.roomQuery();
         },*/
        //重置列表，发生在项目ID发送改变时
        resetSelect(){
           /* this.commitSelectRow = []
            this.tempSelectRow = []
            this.deleteRow = []
            this.roomData = []*/
        }


    },
    watch: {
        projectId: function (newVal, oldVal) {
            if(oldVal != null && oldVal != ''){
                this.resetSelect();
            }
        }
    }
}
</script>

